
var prefix = "/pur/group"
$(function() {
	load();
});

function load() {
	$('#exampleTable')
			.bootstrapTable(
					{
						method : 'get', // 服务器数据的请求方式 get or post
						url : prefix + "/list", // 服务器数据的加载地址
						iconSize : 'outline',
						toolbar : '#exampleToolbar',
						striped : true, // 设置为true会有隔行变色效果
						dataType : "json", // 服务器返回的数据类型
						pagination : true, // 设置为true会在底部显示分页条
						singleSelect : false, // 设置为true将禁止多选
						pageSize : 10, // 如果设置了分页，每页数据条数
						pageNumber : 1, // 如果设置了分布，首页页码
						showColumns : false, // 是否显示内容下拉框（选择显示的列）
						sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
						queryParams : function(params) {
							return {
								//说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
								limit: params.limit,
								offset:params.offset,
								groupid:$('#groupid').val(),
								supplierName:$('#supplierName').val(),
								purType:$('#purType').val()
							};
						},
						// 返回false将会终止请求
						columns : [
								{
									checkbox : true
								},
							    {
									visible : false,
									field : 'id', 
									title : '主键' 
								},
							    {
									field : 'groupid', 
									title : '采购合同编号',
									formatter: function(value,row,index){
										return '<a href="/pur/group/getinfo/'+row.id+'" target="_blank">'+row.groupid+'</a>';
									}
								},
								{
									field : 'projectName',
									title : '所属项目'
								},
								{
									field : 'purType',
									title : '采购类型',
									formatter : function(value, row, index){
										if(value==1){
											return '<span class="label label-primary">公司采购</span>';
										}else if(value==2){
											return '<span class="label label-primary">现场采购</span>';
										}else if(value==3){
											return '<span class="label label-primary">分包</span>';
										}else if(value==4){
											return '<span class="label label-primary">其他</span>';
										}
									}
								},
							    {
									field : 'status', 
									title : '状态',
									formatter : function(value, row, index){
										if(row.status==1){
											return '<span class="label label-primary">新创建</span>';
										}else if(row.status==4){
											return '<span class="label label-success">采购确认</span>';
										}else if(row.status==5){
											return '<span class="label label-danger">已删除</span>';
										}
									}
								},
								{
									field : 'outstockPrice',
									title : '是否发货',
									formatter: function(value,row,index){
										var outstockPrice = row.outstockPrice == ""?0:parseFloat(row.outstockPrice).toFixed(2);
										var allPurprice = row.allPurprice == ""?0:parseFloat(row.allPurprice).toFixed(2);
										if(allPurprice-outstockPrice<=0){
											return '<span class="label label-success">已发货</span>';
										}else{
											return '<span class="label label-primary">未发货</span>';
										}
									}
								},
							    {
									field : 'supplierName', 
									title : '供应商',
									formatter: function(value,row,index){
										return '<a href="/pur/group/getsupplier/'+row.supplierId+'" target="_blank">'+row.supplierName+'</a>';
									}
								},
								{
									field : 'taxRate',
									title : '税率'
								},
								{
									field : 'allPurprice',
									title : '采购总价',
									formatter: function(value,row,index){
										var allPurprice = row.allPurprice == ""?0:parseFloat(row.allPurprice).toFixed(2);
										return allPurprice;
									}
								},
								{
									field : 'paymentPrice',
									title : '已付金额',
									formatter: function(value,row,index){
										var paymentPrice =parseFloat(row.paymentPrice).toFixed(2);
										return '<a href="/fin/payment/getPaymentByPur/'+row.id+'" target="_blank">'+paymentPrice+'</a>';
									}
								},
								{
									field : 'invoicePrice',
									title : '已开票金额',
									formatter: function(value,row,index){
										var invoicePrice =parseFloat(row.invoicePrice).toFixed(2);
										return '<a href="/fin/receiptTick/getInvoiceByitm/'+row.id+'" target="_blank">'+invoicePrice+'</a>';
									}
								},
								{
									field : 'contractUsername',
									title : '合同签订人'
								},
								{
									field : 'remark',
									title : '备注'
								},
								{
									title : '操作',
									field : 'operation',
									align : 'center',
									formatter : function(value, row, index) {
										var e = '<a class="btn btn-primary btn-sm '+s_edit_h+'" href="#" mce_href="#" title="编辑" onclick="edit(\''
											+ row.id
											+ '\')"><i class="fa fa-edit"></i></a> ';
										var c = '<a class="btn btn-success btn-sm '+s_edit_h+'" href="#" mce_href="#" title="确认" onclick="confirm(\''
											+ row.id
											+ '\')"><i class="fa fa-check"></i></a> ';
										var d = '<a class="btn btn-warning btn-sm '+s_remove_h+'" href="#" title="作废"  mce_href="#" onclick="remove(\''
											+ row.id
											+ '\')"><i class="fa fa-remove"></i></a> ';
										if(row.status==1){
											return e+ c + d ;
										}
									}
								} ]
					});
}
function reLoad() {
	$('#exampleTable').bootstrapTable('refresh');
}
function add() {
	layer.open({
		type : 2,
		title : '增加',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/add' // iframe的url
	});
}
function edit(id) {
	layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/edit/' + id // iframe的url
	});
}
function remove(id) {
	layer.confirm('确定要作废选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/remove",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}
function confirm(id) {
	layer.confirm('确定要确认选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/confirm",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}


function exportData() {
	var url =prefix + '/exportData';
	var groupid=$('#groupid').val();
	var supplierName=$('#supplierName').val();
	var purType=$('purType').val();
	if(supplierName != null){
		if (url.indexOf('?') > -1) {
			url += '&supplierName=' + supplierName;
		} else {
			url += '?supplierName=' + supplierName;
		}
	}
	if(groupid != null){
		if (url.indexOf('?') > -1) {
			url += '&groupid=' + groupid;
		} else {
			url += '?groupid=' + groupid;
		}
	}
	if(purType != null){
		if (url.indexOf('?') > -1) {
			url += '&purType=' + purType;
		} else {
			url += '?purType=' + purType;
		}
	}
	var form = document.createElement('form');
	form.setAttribute('action', url);
	form.setAttribute('method', 'post');
	form.setAttribute('style', 'display:none');
	document.body.appendChild(form);
	form.submit();
	document.body.removeChild(form)
}